package com.ynny.jdbc01;

import com.mysql.cj.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCDemo06 {

    public static void main(String[] args) throws SQLException {

        Driver driver = new Driver();
        DriverManager.registerDriver(driver);
        String url = "jdbc:mysql://localhost:3306/db01";
        String userName = "root";
        String password = "root";
        Connection connection = DriverManager
                .getConnection(url, userName, password);
        //写sql
        //占位符 赋值
        String sql = "delete from book where id = ?";
        //4.准备一个PreparedStatement对象 装载运输SQL语句到数据库中
        PreparedStatement preparedStatement =
                connection.prepareStatement(sql);
        //4.1给占位符赋值
        preparedStatement.setString(1,"6");

        //5.执行sql语句  ---->查询用的是 executeQuery()
        //               --->修改 添加 删除 executeUpdate();
        int i = preparedStatement.executeUpdate();
        System.out.println(i);
        //6.关闭资源
        preparedStatement.close();
        connection.close();

    }
}
/**
 * 常见的错误：
 * 1.Access denied for user 'root'@'localhost' (using password: YES)   用户名密码错误
 * 2.Unknown database 'db0198723'  找不到此数据库
 * 3.Table 'db01.book324324' doesn't exist  找不到这个表
 * 4.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
 *    to use near 'id = '6'' at line 1
 *
 * 5.Parameter index out of range (2 > number of parameters, which is 1).
 * 6.No suitable driver found for jdbc//:mysql://localhost:3306/db01
 */
